package com.tarena.csmall.seckill;

import com.tarena.csmall.seckill.init.SeckillInitializer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages ={
    "com.tarena.csmall.seckill",
    "cn.tedu.mall.common.exception",
    "cn.tedu.mall.common.utils"})
@MapperScan("com.tarena.csmall.seckill.mapper")
@EnableDubbo
public class SeckillApplicaiton {
    public static void main(String[] args) {
        //保留一个添加预热的监听器位置,项目每次启动,都在redis做数据的预热
        SpringApplication springApplication
                =new SpringApplication(SeckillApplicaiton.class);
        springApplication.addListeners(new ApplicationListener<ApplicationReadyEvent>(){
            //我们可以在这个方法中,监听很多种不同的springboot启动状态
            //通过监听器泛型 ApplicationReadyEvent 表示springboot启动成功之后的状态
            @Override public void onApplicationEvent(ApplicationReadyEvent event) {
                System.out.println("springboot启动成功");
                //预热数据的初始化
                ConfigurableApplicationContext context = event.getApplicationContext();
                //context包含了所有spring 容器bean对象
                SeckillInitializer initializer = context.getBean(SeckillInitializer.class);
                initializer.init();
                //使用initializer开启一个定时任务,每隔5秒钟,执行一次,将stock库存数 重新覆盖回redis
                initializer.stockTime();
            }
        });
        springApplication.run(args);
    }
}
